<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zhangyajun <448901948@qq.com>
// +----------------------------------------------------------------------

namespace app\common\extend;

use app\common\controller\Upload;

class Cxexp{
    //  解析字段
    function get_form($data){
        if(webCacheHas('artmodel'.$data['mid'])){
            $model = webCacheGet('artmodel'.$data['mid']);
        }else{
            $model = \model('ArtModel')->order('sort desc,id asc')->select();
            foreach ($model as $val){
                if($val['id'] == $data['mid']){
                    $model = $val;
                    webCacheSet('artmodle'.$data['mid'],$model);
                    break;
                }
            }
        }
        if(!is_array($data)){
            $data = $data->toArray();
        }
        $newcont = db("article_content_{$data['mid']}")->where('aid',$data['aid'])->find();
        $data = array_merge($newcont,$data);
        $upmodel = new Upload();
        $model = unserialize($model['config']);
        $fielddb = $model['field_db'];
        foreach ($fielddb as $k => $v){
            switch ($v['formtype']){
                case 'textarea':
                    $data[$k] = preg_replace('/<br>/',"\r",$data[$k]);
                    break;
                case 'upfile':
                    $data[$k] = $upmodel->editadd($data[$k],false);
                    break;
                case 'chinacode':
                    $data[$k] = explode(',',$data[$k]);
                    break;
                case 'updimg':
                    $data[$k] = $upmodel->editadd($data[$k],false);
                    break;
                case 'shopfile':
                    if(!empty($data[$k])){
                        $_newdata = unserialize($data[$k]);
                        foreach ($_newdata as $k1 => $v1){
                            $v1['file'] = explode(',',$v1['value']);
                            $newdata[$k1] = $v1;
                        }
                        $data[$k] = $newdata;
                    }
                    break;
                case 'shopsales':
                    if(!empty($data[$k])){
                        $_newdata = unserialize($data[$k]);
                        foreach ($_newdata as $k1 => $v1){
                            $v1['file'] = explode(',',$v1['value']);
                            $newdata[$k1] = $v1;
                        }
                        $data[$k] = $newdata;
                    }
                    break;
                case 'time':
                    $data[$k] = date('Y-m-d H:m:s', $data[$k]);
                    break;
                case 'articleaid':
                    if(!empty($data[$k])){
                        $aids = unserialize($data[$k]);
                        $aiddata = null;
                        if(is_array($aids)){
                            foreach ($aids as $k1 => $v1){
                                $v1 = model('Article')->where('aid',$v1)->find();
                                $v1 = $this->get_form($v1);
                                $aiddata[] = $v1;
                            }
                        }else{
                            $aiddata = model('Article')->where('aid',$aids)->find();
                            $aiddata = $this->get_form($aiddata);
                        }
                        $data[$k] = $aiddata;
                    }
                    break;
                case 'userdatauid':
                    if(!empty($data[$k])){
                        $uids = unserialize($data[$k]);
                        if(is_array($uids)){
                            foreach ($uids as $k1 => $v1){
                                if(empty($v1)){
                                    continue;
                                }
                                $userdata = \model('UserData')->where('uid',$v1)->find()->toArray();
                                $userdata['group_name'] = model('AuthGroup')->where('id',$userdata['group_id'])->value('title');
                                unset($userdata['upassword']);
                                $userlist[$k1] = $userdata;
                            }
                            $data[$k] = $userlist;
                        }else{
                            $userdata = \model('UserData')->where('uid',$uids)->find()->toArray();
                            $userdata['group_name'] = model('AuthGroup')->where('id',$userdata['group_id'])->value('title');
                            unset($userdata['upassword']);
                            $data[$k] = $userdata;
                        }
                    }
                    break;
                case 'shopmoney':
                    if(!empty($data[$k])){
                        $data[$k] = unserialize($data[$k]);
                    }
                    break;
                case 'uptxt':
                    $data[$k] = $this->editswitch($data[$k]);
                    break;
                case 'upimg':
                    $data[$k] = $this->editswitch($data[$k]);
                    break;
                case 'upmv':
                    $data[$k] = $this->editswitch($data[$k]);
                    break;
            }
        }
        return $data;
    }
    //  处理switch多条件筛选
    protected function editswitch($data){
        $upmodel = new Upload();
        $data = explode('&@&@&',$data);
        $arr = '';
        foreach ($data as $key => $val){
            if(!empty($val)){
                $arr[] = explode('@@@',$val);
            }
        }
        $data = null;
        if(!empty($arr)){
            foreach ($arr as $value){
                $arrs['title'] = $value[0];
                $arrs['value'] = $upmodel->editadd($value[1],false);
                $data[] = $arrs;
            }
        }
        return $data;
    }
}
